package com.sf.asp.core;


import com.sf.asp.constants.enumeration.ResponseStatusEnum;
import com.sf.asp.entity.vo.ResponseInfo;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


@RestControllerAdvice
public class GlobalException {



    @ExceptionHandler(Exception.class)
    public ResponseInfo globalException(Exception e){
        return ResponseInfo.error(ResponseStatusEnum.INTERNAL_ERROR.getCode(), e.getMessage());
    }

    @ExceptionHandler(BindException.class)
    public ResponseInfo validatedDataException(BindException e){
        String errorMsg = e.getAllErrors().get(0).getDefaultMessage();
        return ResponseInfo.error(ResponseStatusEnum.BAD_REQUEST.getCode(), errorMsg);
    }
}
